package org.example.carplatoon.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.example.carplatoon.common.R;
import org.example.carplatoon.entity.carBind;
import org.example.carplatoon.entity.carPlatoon;
import org.example.carplatoon.entity.user;
import org.example.carplatoon.service.carBindService;
import org.example.carplatoon.service.userService;
import org.example.carplatoon.service.carPlatoonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RequestMapping
@RestController
public class carBindController {

    @Autowired
    private carBindService carBindService;

    @Autowired
    private carPlatoonService carPlatoonService;

    @Autowired
    private userService userService;


    /**
     * 根据车队id查询车队内所有成员信息
     * @param platoonId
     * @return
     */
    @GetMapping("/getPointInPlatoon")
    @Transactional
    public R<List<carBind>> getPointInPlatoon(@RequestParam("platoonId") long platoonId) {

        LambdaQueryWrapper<carBind> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(carBind::getPlatoonId, platoonId);
        queryWrapper.orderByAsc(carBind::getCreateTime);
        List<carBind> list = carBindService.list(queryWrapper);
        if (list.isEmpty()) {
            return R.error("该车队暂无车辆");
        }
        return R.success("success",list);
    }

    @GetMapping("/getUserPlatoon")
    @Transactional
    public R<Map<String,Object>> getUserPlatoon(long userId){


        Map<String,Object> map=new HashMap<>();
        //查carbind表，是否这个用户已经加入车队
        LambdaQueryWrapper<carBind> carBindLambdaQueryWrapper=new LambdaQueryWrapper<>();
        carBindLambdaQueryWrapper.eq(carBind::getUserId,userId);
        carBind one = carBindService.getOne(carBindLambdaQueryWrapper);
        if(one==null){
            return R.error("这个用户未加入车队");

        }else{
            Long platoonId = one.getPlatoonId();
            LambdaQueryWrapper<carBind> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(carBind::getPlatoonId, platoonId);
            queryWrapper.orderByAsc(carBind::getCreateTime);
            List<carBind> list = carBindService.list(queryWrapper);
            if (list.isEmpty()) {
                return R.error("该车队暂无车辆");
            }
            map.put("records",list);

            //直接查车队表
            LambdaQueryWrapper<carPlatoon> carPlatoonLambdaQueryWrapper=new LambdaQueryWrapper<>();
            carPlatoonLambdaQueryWrapper.eq(carPlatoon::getId,platoonId);
            carPlatoon one1 = carPlatoonService.getOne(carPlatoonLambdaQueryWrapper);
            map.put("id",one1.getId().toString());
            map.put("carNum",one1.getCarNum());
            map.put("startPoint",one1.getStartPoint());
            map.put("endPoint",one1.getEndPoint());
            map.put("platoonSpacing",one1.getPlatoonSpacing());
            map.put("platoonSpeed",one1.getPlatoonSpeed());


//            System.out.println("我笑了");

            //查用户表,返回该车队的领头人的姓名name,并封装在结果中
            Long userId1 = one1.getUserId();
            LambdaQueryWrapper<user> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            LambdaQueryWrapper<user> eq = lambdaQueryWrapper.eq(user::getId, userId1);
            user one2 = userService.getOne(lambdaQueryWrapper);
            String name = one2.getName();

            map.put("name",name);
        }

        return R.success("返回成功",map);
    }




}
